/**
 * @author 
 * @time 
 * @description 配置路由拦截
 */
 import { commonDefinition } from "../store/index";
 import { deCode } from "../utils/index";
 export default function interceptor(to, _, next) {
    if(window.plus){
        if (to.path === '/login' || to.path === '/') {
            plus.navigator.setStatusBarBackground('#fafafa')
            plus.navigator.setStatusBarStyle('dark')
        } else {
            plus.navigator.setStatusBarBackground('#ed7b2f')
            plus.navigator.setStatusBarStyle('light')
        }
    }else{
        document.addEventListener('plusready', function () {
            if (to.path === '/login' || to.path === '/') {
                plus.navigator.setStatusBarBackground('#fafafa')
                plus.navigator.setStatusBarStyle('dark')
            } else {
                plus.navigator.setStatusBarBackground('#ed7b2f')
                plus.navigator.setStatusBarStyle('light')
            }
        }, false);
    }

     if (to.meta.noLogin) {
         next()
         return
     }
     let cache = sessionStorage.getItem('login-token')
     if (!cache) {
         next('/login')
         return
     }
     /** 一个代码如果容易出现错误，那么要捕获异常 */
     try {
         cache = deCode(cache)
         if (!cache) {
             next('/login')
             return
         }
         cache = cache.split('---')
         /** 保证token数据的时效性 */
         let passTime = Date.now() - cache[1]
         if (passTime > 2 * 60 * 60 * 1000) {
             next('/login')
             return
         }
         /** 保证token的正确性--读取sessionStorage和store中的token数据进行对比 */
         let token = commonDefinition().getToken
         if (cache[0] !== token) {
             next('/login')
             return
         }
         next()
     } catch(e) {
         console.log('------------------- token有问题')
         next('/login')
     }
 }